package exampleQuestions;

import java.util.Arrays;

public class ConvertingArray {

	/**
	 * Given an array of size 2N, such that its elements are as follows:
	 * 
	 * {a1,a2,a3,...,an,b1,b2,b3...,bn}
	 * 
	 * You have to rearrange this array, without using any extra array such that
	 * resulting array would be:
	 * 
	 * {a1,b1,a2,b2,...,an,bn}
	 */
	public static void main(String[] args) {

		String[] array = new String[] { "a1", "a2", "a3", "b1", "b2", "b3" };
		String[] s = new String[] { "A", "B", "C", "D", "1", "2", "3", "4" };

		/*
		 * if(array.length > 2) { convert(0, array.length-1, array); }
		 */

		modify(s);

		System.out.println(Arrays.toString(s));
	}

	private static void modify(String[] array) {
		
		int n = array.length/2;
		int jumps = n-1;
		for(int i=n; i<array.length-1; i++) {
			int j=0;
			while (j<jumps) {
				swap(i-j, array);
				j++;
			}
			jumps--;
		}
		
	}

	private static void swap(int i, String[] array) {
		
		String tmp = array[i];
		array[i] = array[i-1];
		array[i-1] = tmp;
		
	}

	private static void convert(int l, int r, String[] array) {

		int mid = (r - l) >> 1;

		int leftMid = (mid - l) >> 1;
		int rightMid = mid + 1 + (r - (mid + 1)) >> 1;

		for (int i = mid + 1, j = leftMid; i < rightMid; i++, j++) {
			swap(array, j++, i);
		}

		if (r - (mid + 1) >= 2) {
			convert(mid + 1, r, array);
		}
		if (mid - (l) >= 2) {
			convert(l, mid, array);
		}

	}

	private static void swap(String[] array, int i, int j) {
		String tmp = array[i];
		array[i] = array[j];
		array[j] = tmp;
	}

}
